﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameEngine.Scenes;
using Microsoft.Xna.Framework;
using GameEngine.Entities;
using PvT.Entities;
using GameEngine.Managers.IManagers;
using PvT.Scenes.Screens;
using Microsoft.Xna.Framework.Audio;

namespace PvT.Scenes
{
    public class AnimationScene : Scene
    {
        private List<AbstractEntity> sceneObject = new List<AbstractEntity>();
        private AbstractEntity animationSprite;
        private AbstractEntity skynet;

        private double lastAnimationUpdate = 10.0;
        private double updateIntervall = 1.0;
        private int changedToAnimation = 0;

        private string sound1 = "Sound/illBeBack";
        private string sound2 = "Sound/hastaLaVistaBaby";

        /// <summary>
        /// Initializes a new instance of the <see cref="AnimationScene" /> class.
        /// </summary>
        /// <param name="game">The game.</param>
        public AnimationScene(Game game):base(game)
        {
            animationSprite = new AnimationEntity(new List<GameEngine.EntityComponents.IEntityComponent>(), true, true, new Vector2(225, 125), game);
            skynet = new Skynet(new List<GameEngine.EntityComponents.IEntityComponent>(), true, true, new Vector2(350, 250), game, true);
            animationSprite.LoadContent();
            skynet.LoadContent();
            sceneObject.Add(skynet);
            ((IAudioManager)game.Services.GetService(typeof(IAudioManager))).StopLoopedSound("looped");
            try
            {
                SoundEffect soundEffect1 = game.Content.Load<SoundEffect>(sound1);
                ((IAudioManager)game.Services.GetService(typeof(IAudioManager))).AddSoundEffect(soundEffect1, sound1);

                SoundEffect soundEffect2 = game.Content.Load<SoundEffect>(sound2);
                ((IAudioManager)game.Services.GetService(typeof(IAudioManager))).AddSoundEffect(soundEffect2, sound2);
            }
            catch (ArgumentNullException)
            {
            }
        }

        /// <summary>
        /// Updates the AnimationScene
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if ((gameTime.TotalGameTime.TotalSeconds - lastAnimationUpdate) > updateIntervall)
            {
                lastAnimationUpdate = gameTime.TotalGameTime.TotalSeconds;
                changedToAnimation++;
                if (changedToAnimation == 3)
                {
                    ((IAudioManager)game.Services.GetService(typeof(IAudioManager))).PlaySound(sound1, 1.0f);
                }
                else if (changedToAnimation == 6)
                {
                    ((IAudioManager)game.Services.GetService(typeof(IAudioManager))).PlaySound(sound2, 1.0f);
                }
                else if(changedToAnimation == 9){
                    
                    // switch picture
                    sceneObject = new List<AbstractEntity>();
                    sceneObject.Add(animationSprite);
                }
                else if(changedToAnimation == 14)
                {
                    ((ISceneManager)(game.Services.GetService(typeof(ISceneManager)))).ClearScenes();
                    Scene menuScene = new MainMenuScreen(game);
                    ((ISceneManager)(game.Services.GetService(typeof(ISceneManager)))).PushScene(menuScene);
                }
            }
        }

        /// <summary>
        /// Draws the animationScene
        /// </summary>
        /// <param name="spriteBatch">The sprite batch.</param>
        /// <param name="gameTime">The game time.</param>
        public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, GameTime gameTime)
        {
            sceneObject.ElementAt(0).Draw(spriteBatch, gameTime);
        }


        /// <summary>
        /// Gets the scene objects.
        /// </summary>
        /// <returns></returns>
        public override List<AbstractEntity> GetSceneObjects()
        {
            return sceneObject;
        }
    }
}
